package com.ms.common.util;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtil {


	/**
	* @ClassName: Sendmail
	* @Description: 发送Email
	* @author: 孤傲苍狼
	* @date: 2015-1-12 下午9:42:56
	*
	*/ 

	    /**
	     * @param args
	     * @throws Exception 
	     */
	    public static void sendMail(String filePath,String url) throws Exception {
	        
	        Properties prop = new Properties();
	        prop.setProperty("mail.host", "smtp.exmail.qq.com");
	        prop.setProperty("mail.transport.protocol", "smtp");
	        prop.setProperty("mail.smtp.auth", "true");
	       // prop.setProperty("mail.smtp.localhost", "127.0.0.1");
	        //使用JavaMail发送邮件的5个步骤
	        //1、创建session
	        Session session = Session.getInstance(prop);
	        //开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
	        session.setDebug(true);
	        //2、通过session得到transport对象
	        Transport ts = session.getTransport();
	        //3、连上邮件服务器
	        ts.connect("smtp.exmail.qq.com", "chenlei@mysongktv.com", "cL354903651");
	        //4、创建邮件
	        Message message = createMixedMail(session,filePath, url);
	        //5、发送邮件
	        ts.sendMessage(message, message.getAllRecipients());
	        ts.close();
	    }
	    
	    /**
	    * @Method: createMixedMail
	    * @Description: 生成一封带附件和带图片的邮件
	    * @Anthor:孤傲苍狼
	    *
	    * @param session
	    * @return
	    * @throws Exception
	    */ 
	    public static MimeMessage createMixedMail(Session session,String filePath,String url) throws Exception {
	    	Date now_time = new Date();
			System.out.println("nowDate==============="+now_time);
			Calendar calendar = Calendar.getInstance();	
			calendar.setTime(now_time);
			calendar.add(calendar.DATE,-1);
			Date now_time2 = calendar.getTime();
	    	String dt=DateUtils.formatDate(now_time2);
	    	
	        //创建邮件
	        MimeMessage message = new MimeMessage(session);
	        
	        //设置邮件的基本信息
	        message.setFrom(new InternetAddress("chenlei@mysongktv.com"));
	        message.setRecipient(Message.RecipientType.TO, new InternetAddress(url));
	        message.setSubject(dt+"歌曲点唱次数统计");
	        
	        //正文
	        MimeBodyPart text = new MimeBodyPart();
	        text.setContent("您好！  严总<br> 这是 "+dt+"的《我想和你唱》歌曲的播放次数相关统计,请查收！","text/html;charset=UTF-8");
	        
	        //图片
//	        MimeBodyPart image = new MimeBodyPart();
//	        image.setDataHandler(new DataHandler(new FileDataSource(new File("E:\\info.xls"))));
//	        image.setContentID("info.xls");
	        
	        //附件1
	        MimeBodyPart attach = new MimeBodyPart();
	        DataHandler dh = new DataHandler(new FileDataSource(new File(filePath)));
	        attach.setDataHandler(dh);
	        attach.setFileName(dh.getName());
	        
//	        //附件2
//	        MimeBodyPart attach2 = new MimeBodyPart();
//	        DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
//	        attach2.setDataHandler(dh2);
//	        attach2.setFileName(MimeUtility.encodeText(dh2.getName()));
	        
	        //描述关系:正文和图片
	        MimeMultipart mp1 = new MimeMultipart();
	        mp1.addBodyPart(text);
	     //   mp1.addBodyPart(image);
	        mp1.setSubType("related");
	        
	        //描述关系:正文和附件
	        MimeMultipart mp2 = new MimeMultipart();
	        mp2.addBodyPart(attach);
	   //     mp2.addBodyPart(attach2);
	        
	        //代表正文的bodypart
	        MimeBodyPart content = new MimeBodyPart();
	        content.setContent(mp1);
	        mp2.addBodyPart(content);
	        mp2.setSubType("mixed");
	        
	        message.setContent(mp2);
	        message.saveChanges();
	        
	        message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));
	        //返回创建好的的邮件
	        return message;
	    }
	}
	
	
	
